package com.xsp.mybatisxmlsqlparser.multidatabase;

import cn.hutool.core.util.StrUtil;
import com.xsp.mybatisxmlsqlparser.replacer.SpecialMarkReplacer;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Function;
import net.sf.jsqlparser.parser.CCJSqlParserDefaultVisitor;
import net.sf.jsqlparser.parser.CCJSqlParserTreeConstants;
import net.sf.jsqlparser.parser.SimpleNode;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class SQLModifier extends CCJSqlParserDefaultVisitor {

    private final DbCharacteristics dbCharacteristics;
    private final SQLModifierUtils sqlModifierUtils;

    public SQLModifier(DbCharacteristics dbCharacteristics, SQLModifierUtils sqlModifierUtils) {
        this.dbCharacteristics = dbCharacteristics;
        this.sqlModifierUtils = sqlModifierUtils;
    }

    @Override
    public Object visit(SimpleNode node, Object data) {
        Object value = node.jjtGetValue();
        String columnMark = dbCharacteristics.getColumnMark();
        if (node.getId() == CCJSqlParserTreeConstants.JJTTABLENAME) {
            Table table = (Table) value;
            if (StrUtil.isNotBlank(columnMark)) {
                String newTableName = table.getName();
                if (sqlModifierUtils.columnMarkList.stream().anyMatch(table.getName()::contains)) {
                    //清除引号
                    newTableName = sqlModifierUtils.removeQuotes(newTableName);
                }
                if (!table.getName().contains(columnMark)) {
                    table.setName(columnMark + newTableName + columnMark);
                }
            }
            if (node.jjtGetParent().getId() == CCJSqlParserTreeConstants.JJTPLAINSELECT) {
                if (table.getSchemaName() == null) {
                    if (StrUtil.isNotBlank(dbCharacteristics.getDefaultSchema())) {
                        table.setSchemaName(dbCharacteristics.getDefaultSchema());
                    }
                }
            }
        }
        //参数
/*        if (node.getId() == CCJSqlParserTreeConstants.JJTREGULARCONDITION) {
            log.info("==> EXPRESSION: {}", value);
        }*/
        if (node.getId() == CCJSqlParserTreeConstants.JJTFUNCTION) {
            Function function = (Function) value;
            log.info("==> FUNCTION: {}", function.getName());
            log.info("==> PARAMETERS: {}", function.getParameters());
            log.info("==> ATTRIBUTES: {}", function.getAttributeColumn());
        }
        if (node.getId() == CCJSqlParserTreeConstants.JJTCOLUMN) {
            Column column = (Column) value;
            if (SpecialMarkReplacer.containsWellMarkString(column.getColumnName(),
                    SpecialMarkReplacer.WELLMARKSTRINGA,
                    SpecialMarkReplacer.DOLLARSTRINGA,
                    SpecialMarkReplacer.CUSTOM_PLACEHOLDER)) {
                return super.visit(node, data);
            }

            if (StrUtil.isNotBlank(columnMark)) {
                String newColumnName = column.getColumnName();
                if (sqlModifierUtils.columnMarkList.stream().anyMatch(column.getColumnName()::contains)) {
                    //清除引号
                    newColumnName = sqlModifierUtils.removeQuotes(newColumnName);
                }
                if (!column.getColumnName().contains(columnMark)) {
                    column.setColumnName(columnMark + newColumnName + columnMark);
                }
            }
        }
        return super.visit(node, data);
    }
}
